package animations;

import java.awt.Color;
import java.awt.Graphics2D;

import ui.AbsGameElement;

public class MyBall extends AbsGameElement {

	public MyBall(int x, int y, Color c, int d) {
		super();

		this.setColor(c);
		this.setX(x);
		this.setY(y);
		this.setWidth(d);
		this.setHeight(d);

		this.yOld = this.y;
		this.vInit = 0;
		this.movingDir = 1;
		yMax = 510;
		this.lostEnergy = 0.05;
	}

	@Override
	public synchronized void drawBody(Graphics2D g) {
		g.setColor(getColor());
		g.fillOval(getX(), getY(), getWidth(), getHeight());
	}

	@Override
	public synchronized void doMove(double deltaTime) {
		double dT = deltaTime / 1000000L; // nano s to ms
		if(movingDir == 0) {
			return;
		}

		double deltaV = ACCELERATION * dT;
		double deltaY = (vInit * dT + 0.5 * ACCELERATION * Math.pow(dT, 2)) / PIXEL;

		vInit += deltaV * movingDir;
		y += deltaY * movingDir;

		if((y + width) > yMax) {
			movingDir = -1;
			if(vInit > lostEnergy) {
				vInit -= lostEnergy;
			}
			else {
				movingDir = 0;
			}
		}

		if(vInit < 0) {
			movingDir = 1;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null || !(obj instanceof MyBall)) {
			return false;
		}

		MyBall myBall = (MyBall) obj;
		boolean flag = true;
		flag &= (this == myBall);
		flag &= (this.x == myBall.x);
		flag &= (this.y == myBall.y);
		flag &= (this.width == myBall.width);
		flag &= (this.height == myBall.height);
		flag &= (this.color == myBall.color);

		return flag;
	}

}
